<?php
namespace app\admin\controller;

use app\common\model\Adv as AdvModel;
use app\common\model\Advimage as AdvimageModel;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 导航管理
 * Class Nav
 * @package app\admin\controller
 */
class Adv extends AdminBase
{

    protected $adv_model;
    protected $advimage_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->adv_model = new AdvModel();
        $this->advimage_model = new AdvimageModel();
        $adv_level_list = $this->adv_model->getLevelList();
        $this->assign('adv_level_list', $adv_level_list);
    }

    /**
     * 广告管理
     * @return mixed
     */
    public function index()
    {
        $results = $this->advimage_model->field('a.name,i.*')->alias('i')->join('think_adv a','a.id = i.cid')->where("i.status = 0")->select();
        $this->assign("results",$results);
        return $this->fetch();
    }

    /**
     * 添加广告
     * @param string $pid
     * @return mixed
     */
    public function add($pid = '')
    {
        return $this->fetch('add', ['pid' => $pid]);
    }

    /**
     * 保存广告
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            if(empty($data['url'])){
                $data['url'] = '';
            }
            $validate_result = $this->validate($data, 'Article');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                unset($data['content']);
                if ($this->advimage_model->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑广告
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $adv = $this->advimage_model->find($id);
        return $this->fetch('edit', ['adv' => $adv]);
    }

    /**
     * 更新广告
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $data['create_time'] = strtotime($data['create_time']);
            $id = $data['id'];
            unset($data['id']);
            unset($data['content']);
            $validate_result = $this->validate($data, 'Article');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->advimage_model->where("id = {$id}")->update($data) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除广告
     * @param $id
     */
    public function delete()
    {
        $data = $this->request->param();
        $result = $this->advimage_model->where("id = {$data['id']}")->update(["status"=> 1]);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}